import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Home from '@/views/Home'
import Users from '@/views/Users'
import Right from '@/views/right/Right'
import Role from '@/views/right/Role'
import Categories from '@/views/good/Categories'
import Params from '@/views/good/Params'
import Goods from '@/views/good/Goods'
import Add from '@/views/good/Add'
import Order from '@/views/orders/Order'
import Reports from '@/views/Reports'
Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        redirect: '/users',
        children: [{
            path: '/users',
            name: 'users',
            component: Users
        }, {
            path: '/rights',
            name: 'rights',
            component: Right
        }, {
            path: '/roles',
            name: 'roles',
            component: Role
        }, {
            path: '/categories',
            name: 'categories',
            component: Categories
        }, {
            path: '/params',
            name: 'params',
            component: Params
        }, {
            path: '/goods',
            name: 'goods',
            component: Goods
        }, {
            path: '/goods/add',
            name: 'add',
            component: Add
        }, {
            path: '/orders',
            name: 'orders',
            component: Order
        }, {
            path: '/reports',
            name: 'reports',
            component: Reports
        }]
    },

]

const router = new VueRouter({
    routes
})

// 前置守卫
router.beforeEach((to, from, next) => {
    // 判断是否登录
    if (['/', '/login'].includes(to.path)) {
        next()
    } else {
        if (sessionStorage.getItem('shop_userinfo')) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router